<?php

namespace app\common\model\question;

use app\common\model\Model;
use app\common\model\User;
use traits\model\SoftDelete;

class Order extends Model
{

    use SoftDelete;

    // 表名
    protected $name = 'question_order';

    protected $dateFormat = 'Y-m-d H:i:s';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'datetime';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'type_name',
        'status_name',
    ];

    const STATUS = [
        self::STATUS_NON_PAY => '未支付',
        self::STATUS_SUCC => '已支付',
    ];

    const STATUS_SUCC = 1;
    const STATUS_NON_PAY = 0;

    public function getTypeNameAttr($value, $data)
    {
        return $value ?: (Category::VIP[$data['type']] ?? '');
    }

    public function getStatusNameAttr($value, $data)
    {
        return $value ?: (self::STATUS[$data['status']] ?? '');
    }

    public function categoryName()
    {
        return $this->hasOne(Category::class, 'id', 'question_category_id')
            ->bind(['question_category_name' => 'name']);
    }

    public function category()
    {
        return $this->hasOne('Category', 'id', 'question_category_id')->setEagerlyType(0);
    }

    public function user()
    {
        return $this->belongsTo(User::class)->setEagerlyType(0);
    }
}
